package com.roc.web.action.settlemgr;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.TblSettleItem;
import com.roc.service.IApplyMoneyService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
@Component
@Scope("prototype")
@Namespace("/hang")
public class SettlementHangMoneyAction extends PaginationBaseAction{
	private static final long serialVersionUID = 1L;
	private SettlementHangSearchCondition searchCondition;
	private static Logger logger = LogManager.getLogger(SettlementHangMoneyAction.class);
	private File file;
	private String[] multiSel;
	@Autowired
	private IPageService pagenitionService;
	@Autowired
	private IApplyMoneyService applyService;
	/**
	 * 初始化挂起出款页面
	 * @return
	 */
	@Action(value="queryHangInit",results={@Result(name="success",location="/pages/settlementMoney/hangMoneyList.jsp"),
			@Result(name = "error", location = "/error.jsp")})
	public String queryMakeMoneyJsp(){
		Operators op = (Operators) getSession().getAttribute(
			Constant.SESSION_LOGIN_MERCHANT);
		if (op.getRoleType().equals(Role.CLEAR_OPERATORS.getCode())
			|| op.getRoleType().equals(Role.CLEAR_LEAD.getCode())
			|| op.getRoleType().equals(Role.SYSTEM.getCode())) {		
		searchCondition=new SettlementHangSearchCondition();
		String today = Constance.currTime();
		searchCondition.setApplyDateMin(today);
		searchCondition.setApplyDateMax(today);
		return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;	
	}
	/**
	 * 结算完成查询
	 * @return
	 */
	@Action(value="queryHangMoneyJsp",results={@Result(name="success",location="/pages/settlementMoney/hangMoneyList.jsp")})
	public String querySettlementMsgList(){
		try {
			Operators op = (Operators) getSession().getAttribute(
					Constant.SESSION_LOGIN_MERCHANT);
			if (op.getRoleType().equals(Role.CLEAR_OPERATORS.getCode())
					|| op.getRoleType().equals(Role.CLEAR_LEAD.getCode())
					|| op.getRoleType().equals(Role.SYSTEM.getCode())) {
			if(searchCondition == null){
				searchCondition = (SettlementHangSearchCondition) getSearchConditionFromSession();
				if(searchCondition == null){
					searchCondition = new SettlementHangSearchCondition();
				}
			}			
			int rows=pagenitionService.countSQL(searchCondition);
			if(rows>0){
				searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			}
			
			List<TblSettleItem> settleList = new ArrayList<TblSettleItem>();
			dealTheSearchRsList(searchRsList,settleList);
			addActionMessage("当前条件下统计到【"+rows+"】条记录");
			getRequest().setAttribute("settleList", settleList);
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			return SUCCESS;
			}
			addActionError("无权限访问!!!");
		} catch (Exception e) {
			logger.error("结算完成查询异常",e);
			addActionError("结算完成查询异常");
			return ERROR;
		}
		return ERROR;
	}
	private void dealTheSearchRsList(List<Object> searchRsList,
			List<TblSettleItem> settleList) throws Exception {
		try {
			if(searchRsList != null && searchRsList.size() > 0){
				Iterator<Object> it = searchRsList.iterator();
				while(it.hasNext()){
					Object[] obj = (Object[]) it.next();
					TblSettleItem bean = new TblSettleItem();
					bean.setId(Long.parseLong(Constance.Null2EmptyTrim(obj[0])));
					bean.setInsIdCd(Constance.Null2EmptyTrim(obj[1]));
					bean.setMchtCd(Constance.Null2EmptyTrim(obj[2]));
					bean.setMchtName(Constance.Null2EmptyTrim(obj[3]));
					String transAmt = Constance.Null2EmptyTrim(obj[4]);
					if(!"".equals(transAmt)){
						bean.setTransAmt(Double.parseDouble(transAmt));
					}else{
						bean.setTransAmt(0.00);
					}
					String totalNum = Constance.Null2EmptyTrim(obj[5]); 
					if(!"".equals(totalNum)){
						bean.setTotalNum(Integer.parseInt(totalNum));
					}else{
						bean.setTotalNum(0);
					}
					String mchtFee = Constance.Null2EmptyTrim(obj[6]);
					if(!"".equals(mchtFee)){
						bean.setMchtFee(Double.parseDouble(mchtFee));
					}else{
						bean.setMchtFee(0.00);
					}
					String setAmt = Constance.Null2EmptyTrim(obj[7]);
					if(!"".equals(setAmt)){
						bean.setMchtSetAmt(Double.parseDouble(setAmt));
					}else{
						bean.setMchtSetAmt(0.00);
					}
					bean.setBankName(Constance.Null2EmptyTrim(obj[8]));
					bean.setBankCode(Constance.Null2EmptyTrim(obj[9]));
					bean.setAccount(Constance.Null2EmptyTrim(obj[10]));
					bean.setAccountName(Constance.Null2EmptyTrim(obj[11]));
					bean.setAccType(Constance.Null2EmptyTrim(obj[12]));
					bean.setSettleDate(Constance.Null2EmptyTrim(obj[13]));
					bean.setSettleFlag(Constance.Null2EmptyTrim(obj[14]));
					bean.setSetDate(Constance.Null2EmptyTrim(obj[15]));
					bean.setFreezeFlag(Constance.Null2EmptyTrim(obj[16]));
					String freezeAmt = Constance.Null2EmptyTrim(obj[19]);
					if(!"".equals(freezeAmt)){
						bean.setFreezeAmt(Double.parseDouble(freezeAmt));
					}else{
						bean.setFreezeAmt(0.00);
					}
					String unfreezeAmt = Constance.Null2EmptyTrim(obj[20]);
					if(!"".equals(unfreezeAmt)){
						bean.setUnFreezeAmt(Double.parseDouble(unfreezeAmt));;
					}else{
						bean.setUnFreezeAmt(0.00);
					}
					bean.setRemark1(Constance.Null2EmptyTrim(obj[21]));
					bean.setRemark(Constance.Null2EmptyTrim(obj[22]));
					settleList.add(bean);
				}
			}
		}catch (Exception e) {
			throw new Exception("解析数据封装到出款信息实体类异常");
		}
	}
	
	/**
	 * 批量导入初始化页面
	 * @return
	 */
	@Action(value = "startExportJsp", results = { @Result(name = "success", location = "/pages/settlementMoney/exportPayList.jsp")
	,@Result(name = "error", location = "/error.jsp")})	
	public String moreFreezeList()throws Exception
	{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if (op.getRoleType().equals(Role.CLEAR_OPERATORS.getCode())
				|| op.getRoleType().equals(Role.CLEAR_LEAD.getCode())
				|| op.getRoleType().equals(Role.SYSTEM.getCode())) {
				return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;
	}
	/**
	 * 批量导入挂起商户
	 * @return
	 */
	@Action(value = "addPaylist", interceptorRefs = {
			@InterceptorRef(value = "token"),
			@InterceptorRef(value = "rocDefaultStack", params = {
					"fileUpload.allowedExtensions", "xlsx",
					"fileUpload.maximumSize", "100000000" }) }, results = {
			@Result(name = "success",location = "/pages/settlementMoney/exportPayList.jsp"),
			@Result(name = "error", location = "/error.jsp"),
			@Result(name = "invalid.token", location = "/error.jsp")})
	public String addBlackLists()
	{
		try {
			Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			if (op.getRoleType().equals(Role.CLEAR_OPERATORS.getCode())
					|| op.getRoleType().equals(Role.CLEAR_LEAD.getCode())
					|| op.getRoleType().equals(Role.SYSTEM.getCode())) {
				Map<String, Object> resultMap = applyService.insertPaySettlement(file, op);
				if(resultMap.containsKey("result"))
				{	
					Object objMsg=resultMap.get("result");
					addActionMessage(String.valueOf(objMsg));
				}
				return SUCCESS;
			}
			addActionError("无权限访问!!!");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionError("批量导入挂起商户失败~");
			return ERROR;
		}
		return ERROR;		
	}
	
	/**
	 * 出款挂起(复选框模式)
	 * @return
	 */
	@Action(value="hangPaySettlement",results={@Result(name="success",location="/pages/settlementMoney/hangMoneyList.jsp"),
			@Result(name = "error", location = "/error.jsp")})
	public String hangPayListSimple(){
		try {
			Operators op = (Operators) getSession().getAttribute(
					Constant.SESSION_LOGIN_MERCHANT);
			if (op.getRoleType().equals(Role.CLEAR_OPERATORS.getCode())
					|| op.getRoleType().equals(Role.CLEAR_LEAD.getCode())
					|| op.getRoleType().equals(Role.SYSTEM.getCode())) {
				String msg=applyService.hangPaySettlement(multiSel, op);
				addActionMessage(msg);
				return SUCCESS;
			}
			addActionError("无权限访问!!!");
		} catch (Exception e) {
			logger.error("出款挂起异常",e);
			addActionMessage("出款挂起异常");
			return ERROR;
		}
		return ERROR;
	}
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_HANG_PAY_QUERY, searchCondition);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		return (SettlementHangSearchCondition)getSession().getAttribute(SEARCH_SESSION_HANG_PAY_QUERY);
	}

	@Override
	protected String getInitPageSize() {
		return Constance.PAGE_MAX_SIZE+"";
	}

	
	public SettlementHangSearchCondition getSearchCondition() {
		return searchCondition;
	}
	public void setSearchCondition(SettlementHangSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}
	
	public String[] getMultiSel() {
		return multiSel;
	}
	public void setMultiSel(String[] multiSel) {
		this.multiSel = multiSel;
	}
	
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
}
